--
-- Author: ping.wang
-- Date: 2018-08-09 11:38:32
--
-- RewardWelfareGiveRender
--

local ClassRef = app.mvc.createRenderClass(...)
local table_insert = table.insert
local IconRender = require("app.widget.renders.IconRender")

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardWelfareGiveRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    self._tltText = self._ui["Panel/tlt"] --任务名
    self._desText = self._ui["Panel/Text"] --任务描述
    self._imgOver = self._ui.img_over --已领取后显示
    self._bg1 = self._ui["Panel/Panel_bg/bg_1"]
    self._bg2 = self._ui["Panel/Panel_bg/bg_2"]

    self._taskBarPanel = self._ui["Panel/bar"] --任务进度Panel
    self._taskProgressBar = self._taskBarPanel:findChild("LoadingBar") --任务进度条
    self._taskProgressText = self._taskBarPanel:findChild("num") --任务进度条描述

    self._btnReceive = self._ui["Panel/btn_receive"] --接受按钮
    self._finishImg = self._ui["img_over"] --领完后显示

    self._iconList = {}
    for i = 1, 4 do
        local itemIcon = IconRender.create(self._ui["icon_"..i])
        itemIcon:setTipEnable(true)
        table_insert(self._iconList,itemIcon)
    end

    self._btnReceive:addClickEventListener(function()
        if self._data then
            --此处要分 精彩活动和普通活动
            if self._data[2].isMonitor then
                executeFunc(self._onClick, "getMonitorTaskActivityAward", {self._data[2].uuid,self._data[2].taskId})
            else
                executeFunc(self._onClick, "getTaskActivityAward", {self._data[2].uuid,self._data[2].taskId})
            end
        end
    end)
    self:refresh()
end

function ClassRef:refresh()
    if self._data and self._data[2] then
        self:setVisible( true )
        --{uuid = msg.uuid,taskId = v.taskId ,awards = awards, haveGet = v.haveGet,step = v.step, max = v.max}
        local data = self._data[2]

        self._tltText:setString(L(data.name))
        self._desText:setString(L(data.des))


        if data.haveGet then
            self._taskBarPanel:setVisible(false)
            self._btnReceive:setVisible(false)
            self._imgOver:setVisible(true)
            self._bg1:setVisible(true)
            self._bg2:setVisible(false)
        else
            self._bg1:setVisible(false)
            self._bg2:setVisible(true)
            self._imgOver:setVisible(false)
            if data.step < data.max then
                self._taskBarPanel:setVisible(true)
                self._btnReceive:setVisible(false)
                self._taskProgressBar:setPercent(math.floor(data.step/data.max * 100))
                --进度条显示规则修改 因为美术排版只能显示9个字符 超过1000则显示X.XK/X.XK 超过10k则显示xx.xk/xx.xk 超过100k显示xxxk/xxxk 超过1000k显示x.xm/x.xm

                if data.step < 100 and data.max < 100 then
                    self._taskProgressText:setString(string.format(L("activitydes_01"),data.step,data.max))
                else
                    self._taskProgressText:setString(self:_getProgressText(data.step,data.max))
                end

            else
                self._taskBarPanel:setVisible(false)
                self._btnReceive:setVisible(true)
            end
        end

        --icon显示
        for i = 1,#self._iconList do
            if i <= #data.awards then
                self._iconList[i]:setData(data.awards[i])
                self._iconList[i]:setVisible(true)
            else
                self._iconList[i]:setVisible(false)
            end
        end

    else
        self:setVisible( false )
    end
end


function ClassRef:_getProgressText(step, max)
    local str1 = tostring(step)
    if step >= 1000000 then
        str1 = string.format("%.1fM",math.floor(step/100000)/10);
    elseif step >= 100000 then
        str1 = string.format("%.0fK",math.floor(step/1000));
    elseif step >= 10000 then
        str1 = string.format("%.0fK",math.floor(step/1000));
    elseif step >= 1000 then
        str1 = string.format("%.1fK",math.floor(step/100)/10);
    end

    local str2 = tostring(max)
    if max >= 1000000 then
        str2 = string.format("%.1fM",max/1000000);
    elseif max >= 100000 then
        str2 = string.format("%.0fK",max/1000);
    elseif max >= 10000 then
        str2 = string.format("%.0fK",max/1000);
    elseif max >= 1000 then
        str2 = string.format("%.1fK",max/1000);
    end
    return string.format("%s/%s",str1,str2)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
